/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// MCCPanel.cpp : implementation file

#include "stdafx.h"
#include "MCCPanel.h"
#include ".\mccpanel.h"

// MCCPanel

IMPLEMENT_DYNAMIC(MCCPanel, CWnd)
MCCPanel::MCCPanel()
{
	Initialize();

	moving = false;
}

MCCPanel::~MCCPanel()
{
}

void MCCPanel::Initialize(void)
{
	style            = MCCPanel::__pnsRaised;
	backColor        = RGB(0xC0, 0xC0, 0xC0);
	highlightColor   = RGB(0xFF, 0xFF, 0xFF);
	shadowColor      = RGB(0x80, 0x80, 0x80);
	useSystemColors  = false;
	moveParentWindow = false;
	caption          = _T("");
}

BEGIN_MESSAGE_MAP(MCCPanel, CWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

// MCCPanel message handlers

void MCCPanel::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	COLORREF highCol;
	COLORREF shadCol;
	COLORREF backCol;
	CBrush   brush;
	CRect    rect;
	bool     raisedS;

	// Do not call CWnd::OnPaint() for painting messages

	raisedS = (style==__pnsRaised) ? true : false;

	if(useSystemColors){
		highCol = raisedS ? GetSysColor(COLOR_3DHIGHLIGHT) : GetSysColor(COLOR_3DSHADOW);
		shadCol = raisedS ? GetSysColor(COLOR_3DSHADOW)    : GetSysColor(COLOR_3DHIGHLIGHT);
		backCol = GetSysColor(COLOR_3DFACE);
	}
	else{
		highCol = raisedS ? highlightColor : shadowColor;
		shadCol = raisedS ? shadowColor    : highlightColor;
		backCol = backColor;
	}

	brush.CreateSolidBrush(backCol);

	GetClientRect(&rect);
	
	dc.FillRect(&rect, &brush);

	switch(style){
		case __pnsRaised:
		case __pnsLowered:
			dc.Draw3dRect(&rect, highCol, shadCol);
			break;

		default:
			break;
	}

	int dx, dy;
	CSize textSize;
	if(caption!=""){
		textSize = dc.GetTextExtent(caption);
		dx  = rect.Width() - textSize.cx;
		dx /= 2;
		dy  = rect.Height() - textSize.cy;
		dy /= 2;

		dc.SetBkMode(TRANSPARENT);

		if(style==__pnsRaised)
			dc.SetTextColor(shadCol);
		else
			dc.SetTextColor(highCol);
		dc.TextOut(rect.left + dx + 1, rect.top + dy + 1, caption);

		if(style==__pnsRaised)
			dc.SetTextColor(highCol);
		else
			dc.SetTextColor(shadCol);
		dc.TextOut(rect.left + dx, rect.top + dy, caption);
	}
}

void MCCPanel::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWnd::OnLButtonDown(nFlags, point);

	startPoint.x = point.x;
	startPoint.y = point.y;
	stopPoint.x  = point.x;
	stopPoint.y  = point.y;
	moving = true;
}

void MCCPanel::OnLButtonUp(UINT nFlags, CPoint point)
{
	CWnd::OnLButtonUp(nFlags, point);

	moving = false;
}

void MCCPanel::OnMouseMove(UINT nFlags, CPoint point)
{
	CWnd* parentWnd;
	CRect rect;
	int dx, dy;

	CWnd::OnMouseMove(nFlags, point);

	parentWnd = GetParent();
	if(parentWnd==NULL) return;

	if(moving){
		stopPoint.x = point.x;
		stopPoint.y = point.y;

		dx = stopPoint.x - startPoint.x;
		dy = stopPoint.y - startPoint.y;
		if(dx!=0 || dy!=0){
			parentWnd->GetWindowRect(&rect);
			parentWnd->MoveWindow(rect.left + dx, rect.top + dy, rect.Width(), rect.Height(), TRUE);
		}
	}
}
